package com.skyc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.skyc.dao.collectionDao;
import com.skyc.entity.Clothes;
import com.skyc.entity.User;
import com.skyc.service.collectionService;
import com.skyc.service.impl.collectionServiceImpl;

/**
 * Servlet implementation class toindexServlet
 */
@WebServlet("/docollectionServlet")
public class docollectionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		int index = Integer.valueOf(request.getParameter("index"));
		int flag = Integer.valueOf(request.getParameter("flag"));
		int ccid = Integer.valueOf(request.getParameter("ccid"));
		User user = (User) session.getAttribute("user");
		if (user != null) {
			collectionService cd = new collectionServiceImpl();
			List<Clothes> BKList = (List<Clothes>) session.getAttribute("BKList");
			List<Clothes> menList = (List<Clothes>) session.getAttribute("menList");
			List<Clothes> womenList = (List<Clothes>) session.getAttribute("womenList");
			List<String> wocc = (List<String>) session.getAttribute("wocc");
			List<String> cc = (List<String>) session.getAttribute("cc");
			List<String> bkcc = (List<String>) session.getAttribute("bkcc");
			if (flag == 1) {
				if (bkcc.get(index).equals("收藏")) {// 存数据库
					cd.addcollection(user, ccid);
				} else {
					cd.deletecollection(user, ccid);
				}
			}
			for (String n : wocc) {
				System.out.println(n);
			}
			if (flag == 2) {
				if (wocc.get(index).equals("收藏")) {
					cd.addcollection(user, ccid);
				} else {
					cd.deletecollection(user, ccid);
				}
			}
			if (flag == 3) {
				if (cc.get(index).equals("收藏")) {
					cd.addcollection(user, ccid);
				} else {
					cd.deletecollection(user, ccid);
				}
			}
			collectionService ce = new collectionServiceImpl();
			List<Clothes> uc = ce.ucollection(user);
			for (int i = 0; i < menList.size(); i++) {
				if (uc.size() > 0) {
					if (cc.get(i).equals("取消收藏")) {
						cc.set(i, "收藏");
					}
					for (Clothes n1 : uc) {
					
						if (menList.get(i).getCcid() == n1.getCcid()) {
							cc.set(i, "取消收藏");
						}
					}
				} else {
					cc.set(i, "收藏");
				}
			}
			for (int i = 0; i < BKList.size(); i++) {
				if (uc.size() > 0) {
					if (bkcc.get(i).equals("取消收藏")) {
						bkcc.set(i, "收藏");
					}
					for (Clothes n1 : uc) {
						if (BKList.get(i).getCcid() == n1.getCcid()) {
							bkcc.set(i, "取消收藏");
						}
					}
				} else {
					bkcc.set(i, "收藏");
				}
			}
			for (int i = 0; i < womenList.size(); i++) {
				if (uc.size() > 0) {
					if (wocc.get(i).equals("取消收藏")) {
						wocc.set(i, "收藏");
					}
					for (Clothes n1 : uc) {
					
						if (womenList.get(i).getCcid() == n1.getCcid()) {
							wocc.set(i, "取消收藏");
						}
					}
				} else {
					wocc.set(i, "收藏");
				}
			}
			session.setAttribute("bkcc", bkcc);
			session.setAttribute("wocc", wocc);
			session.setAttribute("cc", cc);
			request.getRequestDispatcher("collection_show1.jsp").forward(request, response);
		} else {
			PrintWriter out = response.getWriter();
			out.print("<script>alert('请先登录');location='toindexServlet';</script>");
		}

	}

}
